﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Collections.Specialized;
using TestKooboo.Models;
using TestKooboo.Bizs;
using KB;
using Kooboo.CMS.Sites.Models;
using Kooboo.CMS.Sites.Services;
using KB.RazorTemplate;
using System.Threading.Tasks;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Query;
using Ionic.Zip;
namespace TestKooboo.Controllers
{
    public class TestController : Controller
    {
        public JsonResult KnockOut(int take)
        {
            var list = BizContext.Current.ArticleBiz.GetListOrdered(take).Select(it => new { it.Title, it.Summary, it.Sequence, Date=it.UtcCreationDate.AsLocalDateTime() });
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            if (Request.IsAuthenticated || Request.IsLocal)
            {
                var job = new Jobs.CreateGoogleSiteMap().AttachJob(1000, false);
                return Content(DateTime.Now.Hour.ToString());
            }
            return Content(AppDomain.CurrentDomain.BaseDirectory);

            //var a = new Article();
            //a.Test = "sdfsdf";
            //a.Title = DateTime.Now.ToString() + "Controller";
            //BizContext.Current.ArticleBiz.Add(a);
        }

        public void Zip()
        {
            Response.Clear();
            Response.BufferOutput = false;  // for large files
            Response.ContentType = "application/zip";
            var fileName = "Hello.zip";
            Response.AddHeader("content-disposition", "filename=" + fileName);
            using (ZipFile zip = new ZipFile(System.Text.Encoding.UTF8))
            {
                var files = System.IO.Directory.GetFiles(Server.MapPath("~"));
                zip.AddFiles(files);
                zip.Save(Response.OutputStream);
            }
            Response.Close();
        }
        public string Test()
        {
            var item = "";
            var article = BizContext.Current.ArticleBiz;
            article.CurrentRepository = new Repository("TestKooboo");
            var list = article.GetList();
            Parallel.ForEach(list, it => { item = item + it.Title; });
            return item;
        }
    }
}
